public class Test04 {
    public int minOperations(int[] nums, int x) {

        int sum = 0;
        for(int a : nums){
            sum += a;
        }
        int target = sum - x;
        if(target < 0) return -1;
        //使用滑动窗口求出最长子数组的和为target
        int left = 0, right = 0 ,subSum = 0,maxLength = -1;
        while(right < nums.length){
            subSum += nums[right];
            while( subSum > target){
                subSum -= nums[left];
                left++;
            }
            if(subSum == target){
                maxLength = Math.max(maxLength,right - left + 1);
            }
            right++;
        }
        if(maxLength == -1) return -1;
        return nums.length - maxLength;
    }
}
